<?php 
namespace backend\controllers;
use yii\web\Controller;
use common\models\OpenCard;
use common\models\Yrclass;
use common\models\Teacher;
use common\models\Student;
use common\models\YzWxuser;
use app\Chat\Template;
use yii\data\ActiveDataProvider;

class OpencardController extends Controller{
	
	public function actionIndex() {
		$query = OpenCard::find();
		// dd($openInfo);
		// die;
		 $dataProvider = new ActiveDataProvider([
            'query'=>$query,
            'pagination'=>[
                'pagesize'=>10
            ]
        ]);
		return $this->render("index",['dataProvider'=>$dataProvider]);
	}
	
	/*
	*todo  打卡
	*$name 学生姓名
	*$sid  学生id
	*$cid  班级id
	*$time 剩余课时
	*$person 打卡人
	*$tid  教师id
	 */
	public function punch($sid,$cid,$name,$time,$person="",$tid) {
	 	$openInsert = new OpenCard();
		$openInsert->c_id = $cid;
		$openInsert->s_id = $sid;
		$openInsert->identity = 0;
		$openInsert->name = $name;
		$openInsert->push_time = date('Y-m-d H:i:s',time());
		$openInsert->punch = empty($person)?"管理员":$person; 
		$openInsert->class_time = $time;
		$openInsert->created_at = date('Y-m-d H:i:s',time());
		$openInsert->updated_at = date('Y-m-d H:i:s',time());
		$openInsert->t_id = $tid; 
		$res = $openInsert->save();
		if($res) {
			$this->post($sid);
		}
		// public function actionIndex() {
		// 		$openInsert = new OpenCard();
		// 		$openInsert->c_id = 2;
		// 		$openInsert->s_id = 1;
		// 		$openInsert->identity = 0;
		// 		$openInsert->name = 'lucky';
		// 		$openInsert->push_time = date('Y-m-d H:i:s',time());
		// 		$openInsert->punch = "管理员"; 
		// 		$openInsert->class_time = 2;
		// 		$openInsert->created_at = date('Y-m-d H:i:s',time());
		// 		$openInsert->updated_at = date('Y-m-d H:i:s',time());
		// 		$openInsert->t_id = 2; 

		// 		$res = $openInsert->save();
		// 		dd($res);
	}
		
		
	//发送模板消息
	public function post($sid) {
		$student = Student::findOne($sid);
		//查询家长openid
		$res = $student->hasOne(YzWxuser::className(),['id'=>'p_id'])->asArray()->all();
		// dd($res[0]['openid']);

		$openid = $res[0]['openid'];  
		//发送模板消息
		$tem = new Template($accesstoken,$openid,$template);
		if($tem->send()) {
			$this->redirect(['index']);
		}

	}

	//导出excel
	 public function actionExcel()
    {
        //查询学生表数据
        $result = Student::find()->select(['id','name','sex','age','parent_name','parent_tel','school','card_type','class','subscribe_class','subscribe_mark','subscribe_time','is_binding','mark','created_at','updated_at'])->asArray()->all();
        //查询卡类型作为提示信息
        $card = Card::find()->select('id,name')->asArray()->all();
        $ts = "";
        foreach($card as $c) {
            $ts .= implode(':',$c)."  ";
        }

        // dd($ts);
        // die;
        $dataResult = $result;      //todo:导出数据（自行设置） 
        $headTitle = "<h1>学生信息</h1><p style='color:red;'>&nbsp;卡类型：".$ts."</p><p style='color:red;'>&nbsp;是否绑定服务号：&nbsp;1:已绑定&nbsp;0：未绑定</p>";
        $time = time(); 
        $title = date('Y-m-d',$time)."学生信息表"; 
        $headtitle= "<tr style='height:50px;border-style:none;><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>"; 
        $titlename = "<tr> 
                    <th style='width:70px;' >编号</th> 
                    <th style='width:70px;' >姓名</th> 
                    <th style='width:40px;'>性别</th> 
                    <th style='width:70px;'>年龄</th> 
                    <th style='width:90px;'>父亲姓名</th> 
                    <th style='width:120px;'>父亲号码</th> 
                    <th style='width:200px;'>学校</th> 
                    <th style='width:70px;'>卡类型</th> 
                    <th style='width:70px;'>班级</th> 
                    <th style='width:90px;'>预约课程</th> 
                    <th style='width:200px;'>预约备注</th> 
                    <th style='width:140px;'>预约时间</th> 
                    <th style='width:90px;'>是否绑定服务号</th> 
                    <th style='width:200px;'>备注</th> 
                    <th style='width:140px;'>创建时间</th> 
                    <th style='width:140px;'>更新时间</th> 
                </tr>"; 
           $filename = $title.".xls"; 
           excelData($dataResult,$titlename,$headtitle,$filename); 
           
    }
}



 ?>